<?php
require '../model/Db.php';
$Db = Db::getInstance();
$admin_data = $Db->isadmin();
if(!$admin_data){
	$user_data = $Db->islogin();
	if($user_data['job'] == 3){
		$admin_data = $user_data;
	}
}
if(($admin_data || $user_data['job'] != 0)){
	switch ($_GET['type']) {
		case 1://区版排序
			$sort_arr = explode(',', $_GET['sort']);
			$zone_data = $Db->find([
				table => ['zone'],
				where => '1 order by sort,id'
			]);
			foreach ($zone_data as $key => $value) {
				if($value['id'] == 1){
					continue;
				}
				$Db->update([
					table => 'zone',
					map => [
						sort => $sort_arr[$key-1]
					],
					where => "id = {$value['id']}"
				]);
			}
			echo json_encode([res => true]);
			break;
		
		case 2://种类排序
			$sort_arr = explode(',', $_GET['sort']);
			$kind_data = $Db->find([
				table => ['kind'],
				where => "zid = {$_GET['id']} order by sort,id"
			]);
			foreach ($kind_data as $key => $value) {
				$Db->update([
					table => 'kind',
					map => [
						sort => $sort_arr[$key]
					],
					where => "id = {$value['id']}"
				]);
			}
			echo json_encode([res => true]);
			break;

		case 3://举报帖子设为合法
			$now = date('Y-m-d H:i:s');
			$Db->update([
				table => 'report',
				map => [
					delete_time =>$now
				],
				where => "cid = {$_GET['id']}"
			]);
			echo json_encode([res => true]);
			break;

		case 4://举报帖子删除
			$to = $Db->find([
				table => [content => 'a',bbs_user => 'b'],
				field => 'b.id,a.body,a.title',
				where => "a.uid = b.id and a.id = {$_GET['id']}"
			])[0];
			//发信
			$Db->save([
				table => 'mail',
				map => [
					from_id => 0,
					to_id => $to['id'],
					title => '帖子违规',
					body => "您的帖子 {$to['title']},\n内容:{$to['body']}\n经过核实存在违规,管理员已将您的帖子删除,良好的网络环境需要大家一起守护。"
				]
			]);
			//扣分
			$Db->update([
				table => 'bbs_user',
				map => [
					lv => '#lv-5#'
				],
				where => "id = {$to['id']}"
			]);
			$Db->remove([
				table => 'report',
				flag => true,
				where => "cid = {$_GET['id']}"
			]);
			$Db->remove([
				table => 'content',
				where => "id = {$_GET['id']}"
			]);
			echo json_encode([res => true]);
			break;

		case 'z':
			$isexist = $Db->find([
				table => ['bbs_user'],
				where => "id = {$_GET['uid']}"
			])[0];
			if($isexist){
				$new_obj = json_decode($isexist['follow_me']);
				$z_arr = $new_obj->zids;
				if(!in_array($_GET['id'], $z_arr)){
					$in = true;
					array_push($z_arr, $_GET['id']);
				}else{
					$in = false;
					$index = array_search($_GET['id'], $z_arr);
					array_splice($z_arr,$index,1);
				}
				$new_obj->zids = $z_arr;
				if($in && $isexist['job'] < 2){
					$map['job'] = '2';
				}else{
					if($isexist['job'] != 3 && !$new_obj->zids){
						if($new_obj->mids){
							$map['job'] = '1';
						}else{
							$map['job'] = '0';
						}
						
					}
				}
				$map['follow_me'] = json_encode($new_obj);
				//改用户
				$Db->update([
					table => 'bbs_user',
					map => $map,
					where => "id = {$_GET['uid']}"
				]);
				//改版面
				$m_data = $Db->find([
					table => ['zone'],
					where => "id = {$_GET['id']}"
				])[0];
				$new_obj2 = json_decode($m_data['manager']);
				$u_arr = $new_obj2->uids;
				if(!in_array( $_GET['uid'], $u_arr)){
					
					array_push($u_arr, $_GET['uid']);
				}else{
					
					$index = array_search($_GET['uid'], $u_arr);
					array_splice($u_arr,$index,1);
				}
				$new_obj2->uids = $u_arr;
				$Db->update([
					table => 'zone',
					map => [
						manager => json_encode($new_obj2)
					],
					where => "id = {$_GET['id']}"
				]);
			}
			break;

		case 'm':
			$isexist = $Db->find([
				table => ['bbs_user'],
				where => "id = {$_GET['uid']}"
			])[0];
			if($isexist){
				$new_obj = json_decode($isexist['follow_me']);
				$m_arr = $new_obj->mids;
				if(!in_array($_GET['id'], $m_arr)){
					$in = true;
					array_push($m_arr, $_GET['id']);
				}else{
					$in = false;
					$index = array_search($_GET['id'], $m_arr);
					array_splice($m_arr,$index,1);
				}
				$new_obj->mids = $m_arr;
				if($in && $isexist['job'] < 1){
					$map['job'] = '1';
				}else{
					if($isexist['job'] != 3 && !$new_obj->zids && !$new_obj->mids){
						$map['job'] = '0';
					}
				}
				$map['follow_me'] = json_encode($new_obj);

				//改用户
				$Db->update([
					table => 'bbs_user',
					map => $map,
					where => "id = {$_GET['uid']}"
				]);
				//改版面
				$m_data = $Db->find([
					table => ['module'],
					where => "id = {$_GET['id']}"
				])[0];
				$new_obj2 = json_decode($m_data['manager']);
				$u_arr = $new_obj2->uids;
				if(!in_array( $_GET['uid'], $u_arr)){
					
					array_push($u_arr, $_GET['uid']);
				}else{
					
					$index = array_search($_GET['uid'], $u_arr);
					array_splice($u_arr,$index,1);
				}
				$new_obj2->uids = $u_arr;
				$Db->update([
					table => 'module',
					map => [
						manager => json_encode($new_obj2)
					],
					where => "id = {$_GET['id']}"
				]);
			}
			
			break;

		case 5://站务权限
			$exist = $Db->find([
				table => ['bbs_user'],
				where => "id = {$_GET['uid']}"
			])[0];
			if($exist){
				$zids = json_decode($exist['follow_me'])->zids;
				$mids = json_decode($exist['follow_me'])->mids;
				if($exist['job'] == 3){
					//移除
					if($zids){
						$job = '2';
					}elseif($mids){
						$job = '1';
					}else{
						$job = '0';
					}
				}else{
					//设置
					$job = '3';
				}
				$flag = $Db->update([
					table => 'bbs_user',
					map => [
						job => $job
					],
					where => "id = {$_GET['uid']}"
				]);
				echo json_encode([res => $flag]);
			}else{
				$msg = '用户不存在！';
				$class = 'err';
				$reurl = '?p=2';
				include './view/skip.html';
				exit();
			}
			break;
			
		case 6:  //置顶、史诗
		if(isset($_GET['top'])){
			$ex = $Db->find([
				table => ['content'],
				where => "id = {$_GET['cid']}"
			])[0];
			if(!$ex){
				$msg = '帖子不存在！';
				$class = 'err';
				$reurl = '?p=5';
				include './view/skip.html';
				exit();
			}
			$top = $ex['top'] == '1'?'0':'1';
			$Db->update([
				table => 'content',
				map => [
					top => $top 
				],
				where => "id = {$_GET['cid']}"
			]);
		}elseif(isset($_GET['great'])){
			$ex = $Db->find([
				table => ['content'],
				where => "id = {$_GET['cid']}"
			])[0];
			if(!$ex){
				$msg = '帖子不存在！';
				$class = 'err';
				$reurl = '?p=5';
				include './view/skip.html';
				exit();
			}
			$great = $ex['great'] == '1'?'0':'1';
			$Db->update([
				table => 'content',
				map => [
					great => $great 
				],
				where => "id = {$_GET['cid']}"
			]);
		}
		    break;
		    
		case 7://修改维修、活动受理
		    if($_GET['s']=='hd'){
		        $table = 'hdsb';
		    }elseif($_GET['s']=='wx'){
		        $table = 'gzwx';
		    }else{
		        exit();
		    }
		    $flag = $Db->update([
				table => $table,
				map => [
					is_deal => 1,
					is_do => $_GET['isdo']
				],
				where => "cid = {$_GET['id']}"
			]);
			echo(json_encode([res=>$flag]));
		    break;
	}
}else{
	http_response_code(404);
	echo '<h1 style="color:#337ab7">千呼万唤始出来,犹抱琵琶半遮面</h1>';
}